﻿using System; 

namespace Plugin
{
    public class TransformMacro : MarshalByRefObject
    {
        //See http://wiki.eigenvector.com/index.php?title=Advanced_Preprocessing:_Sample_Normalization for more information on Normalization
        public float[] Execute(ushort[] aInterpolatedWavelengths, float[] AbsorbanceSpectra, float[] FirstDerivativeSpectra, float[] SecondDerivativeSpectra)
        { 
            float[] correctedSpectra = new float[AbsorbanceSpectra.Length];
            float[] smoothedSpectra = new float[AbsorbanceSpectra.Length];
            
            
            try 
            {
            
                //second dir savitkygolaycoefficent
                float[] dir2savitzkyGolayCoefficients = new float[] 
                                                                {
                                                                -0.0216450206935406f, 
                                                                -0.018398268148303f, 
                                                                -0.00865800771862268f, 
                                                                0.00757575873285532f,
                                                                0.030303031206131f, 
                                                                0.030303031206131f, 
                                                                0.00757575873285532f, 
                                                                -0.00865800771862268f, 
                                                                -0.018398268148303f 
                                                                };
                                                        
                 //first dir savitkygolaycoefficent
                float[] dir1savitzkyGolayCoefficients = new float[] 
                                                                   {
                                                                    0f, 
                                                                    -0.066667f, 
                                                                    -0.05f, 
                                                                    -0.03333f, 
                                                                    -0.0166667f,
                                                                    0.01666667f, 
                                                                    0.03333f, 
                                                                    0.05f, 
                                                                    0.066667f
                                                                };
                int n1      = 16; //n-1 number of spectra in training set used to calc avg abs
                float norm2 = 10f;  //Normalize to the sum of the squared value of all variables for the given sample.
            
                //We do not have a varible that exposes the trainingset spectra to the model
                // to overcome this we must calculate the avg externally (excel)
                // then paste the properly formated float array into the varible below
                float[] AvgAbsTrainingsetSpectra = new float[] {0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,-0.01148078f,-0.01492212f,-0.009874135f,-0.0009464016f,0.008522058f,0.01551523f,0.01726697f,0.01367932f,0.001612521f,0.000724941f,0.001665434f,0.002859431f,0.003261144f,0.002494271f,0.001623659f,0.002333636f,0.002821153f,0.0026356f,0.001697792f,0.001026522f,-0.001062814f,-0.004008279f,-0.006275804f,-0.007167444f,-0.006637575f,-0.005471512f,-0.004398838f,-0.003030349f,-0.001913838f,-0.001208082f,-0.0006049667f,-7.701851E-05f,0.0002295468f,0.0003581867f,0.0003654845f,0.0002305042f,0.0001723748f,0.0002165977f,0.0003116839f,0.0003591757f,0.0003361143f,0.0002591237f,0.0001471657f,3.797375E-05f,6.409176E-05f,1.407601E-05f,4.227087E-05f,-2.494454E-05f,-0.0001486912f,-0.0002466645f,-0.0003608093f,-0.0004517622f,-0.0005744882f,-0.0008223932f,-0.0009649061f,-0.001213137f,-0.001446433f,-0.001434514f,-0.001232384f,-0.0007934533f,-0.0001956224f,0.0004356205f,0.00100705f,0.001265764f,0.001284454f,0.001209058f,0.0009952448f,0.0007813182f,0.000662921f,0.000691114f,0.0008455701f,0.001053825f,0.001213623f,0.001282552f,0.001234334f,0.001029164f,0.0005944204f,-5.536899E-05f,-0.0007376727f,-0.001253126f,-0.001467006f,-0.001356708f,-0.0009827446f,-0.0006036758f,-0.0002914891f,-0.000114752f,2.349541E-05f,0.0001539607f,0.0002372283f,0.0002492126f,0.0001549236f,-3.472716E-05f,-0.0001777727f,-0.0003301315f,-0.0003577229f,-0.0002496373f,-6.286055E-05f,0.0001244489f,0.0002556052f,0.0002701618f,0.0001691747f,-2.097897E-05f,-0.0001972225f,-0.0003205296f,-0.0004556533f,-0.0005023219f,-0.0004750527f,-0.0004400741f,-0.0004606303f,-0.00053427f,-0.0007254649f,-0.001050776f,-0.001779094f,-0.002929851f,-0.00450431f,-0.006172199f,-0.007376838f,-0.007596927f,-0.006579204f,-0.00450621f,-0.002045501f,0.0001285337f,0.001443112f,0.002215792f,0.002570911f,0.002715974f,0.00281221f,0.002918467f,0.002977916f,0.002889358f,0.002656447f,0.002598313f,0.002486713f,0.002386554f,0.002264904f,0.002107912f,0.00190523f,0.001652891f,0.001361918f,0.001044699f,0.0007834949f,0.0006013466f,0.0004518307f,0.0003396659f,0.0002564956f,0.0001971098f,0.0001512128f,0.0001107445f,7.974869E-05f,5.582673E-05f,4.57596E-05f,4.164409E-05f,4.098006E-05f,5.347887E-05f,6.728154E-05f,8.048117E-05f,9.598769E-05f,0.0001172321f,0.0001400639f,0.0001580166f,0.0001692683f,0.000162187f,0.0001126784f,3.369106E-05f,-5.597714E-05f,-0.0001242533f,-0.0001511248f,-0.0001351833f,-9.27425E-05f,-4.610978E-05f,-1.381803E-05f,9.676442E-06f,2.403324E-05f,3.762124E-05f,5.692104E-05f,7.973146E-05f,0.0001015719f,0.0001163459f,0.0001229257f,0.0001247539f,0.0001176046f,9.876257E-05f,7.392E-05f,5.158829E-05f,3.41502E-05f,3.085425E-05f,4.731352E-05f,8.085556E-05f,0.0001202361f,0.0001615952f,0.0002074256f,0.0002571456f,0.0003068531f,0.0003571585f,0.0004004119f,0.0004271306f,0.0004240051f,0.0004114127f,0.0004174314f,0.0004616668f,0.0005362486f,0.0005893921f,0.0005436728f,0.0003303823f,-4.404923E-05f,-0.0004940657f,-0.0008735522f,-0.001100092f,-0.001132532f,-0.00101032f,-0.0008308333f,-0.0006645727f,-0.0005442121f,-0.0004762337f,-0.0004292461f,-0.0004075258f,-0.0003739907f,-0.000332078f,-0.0002840878f,-0.0002458347f,-0.000209576f,-0.0001846377f,-0.0001593705f,-0.0001449394f,-0.0001138295f,-8.571427E-05f,-4.951074E-05f,-1.246599E-05f,8.235918E-06f,4.097959E-05f,7.941807E-05f,9.483472E-05f,9.137578E-05f,8.058827E-05f,0.0001603044f,0.0002214601f,0.0002577789f,0.0002842094f,0.0003214106f,0.0003232607f,0.0002613598f,0.0002780282f,0.0004081302f,0.0003762725f,0.0003940165f,0.000349056f,0.0003867424f,0.0001929742f,0.0002991571f,0.0005491069f,0.00065673f,0.0006557186f,0.0006428845f,0.0005136011f,0.00057542f,0.0005667275f,0.0009770361f,0.00159648f,0.002055554f,0.002626002f,0.003656187f,0.004372467f,0.005793958f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f};
           
            
                //calculate mean center spectra and square of the 2normal varible required for method barnes et al (1985)
                //there is a divide by zero bug in the avgabs spectra. It is fixed by over writing the frist and last so many pixels
                //in the SG routine
                for (int pixel = 0; pixel < AbsorbanceSpectra.Length; pixel +=1)
                {
                    try 
                    {
                        smoothedSpectra[pixel] = ((AbsorbanceSpectra[pixel] - AvgAbsTrainingsetSpectra[pixel])); 
                        norm2 += ((AbsorbanceSpectra[pixel] - AvgAbsTrainingsetSpectra[pixel])*(AbsorbanceSpectra[pixel] - AvgAbsTrainingsetSpectra[pixel]));
                    } 
                    catch
                    {         
                        correctedSpectra[pixel] = 0f;
                    }
                }
            
                for (int pixel = 0; pixel < AbsorbanceSpectra.Length; pixel +=1)
                {
                    try 
                    {
                        smoothedSpectra[pixel] = smoothedSpectra[pixel] / (float)(System.Math.Sqrt(norm2 / (float)n1));
                    } 
                    catch
                    {
                        correctedSpectra[pixel] = 0f;
                    }
                 }
                                                        
           
                        
                //Applies the savitzkygolay smoothing filter  
                //if first dir is perfered edit code
                for (int pixel = 0; pixel < (correctedSpectra.Length - dir2savitzkyGolayCoefficients.Length); ++pixel)
                { 
                
                    for (int windowPixel = 1; windowPixel <= 4; ++windowPixel)
                    { 
                        correctedSpectra[pixel + 4] += (smoothedSpectra[pixel + (windowPixel - 1)] * dir2savitzkyGolayCoefficients[4 - (windowPixel - 1)]);
                        correctedSpectra[pixel + 4] += (smoothedSpectra[pixel + windowPixel + 4] * dir2savitzkyGolayCoefficients[9 - windowPixel]);
                    } 
                    correctedSpectra[pixel + 4] += (smoothedSpectra[pixel + 4] * dir2savitzkyGolayCoefficients[0]); 
                } 
            
                for (int pixel = 0; pixel < 50; ++pixel)
                { 
                    correctedSpectra[pixel] = 0f;
                }
            
                for (int pixel = correctedSpectra.Length - 50; pixel < correctedSpectra.Length; ++pixel)
                { 
                    correctedSpectra[pixel] = 0f;
                }
              
            }
            catch 
            {
            
                for (int pixel = 0; pixel < correctedSpectra.Length; ++pixel)
                { 
                    correctedSpectra[pixel] = 0;
                }
            }
            
            return correctedSpectra;
        }
    }
}

